package com.xianggu.tomcat;

import javax.servlet.ServletOutputStream;
import javax.servlet.WriteListener;
import java.io.IOException;
import java.util.Arrays;

/**
 * @author chengjy
 * @date 2025/3/6
 */
public class ResponseServletOutputStream extends ServletOutputStream {

    byte[] bytes = new byte[2048];
    int index = 0;

    @Override
    public boolean isReady() {
        return false;
    }

    @Override
    public void setWriteListener(WriteListener writeListener) {

    }

    @Override
    public void write(int b) throws IOException {
        bytes[index++] = (byte) b;
    }

    public byte[] getBytes() {
        if( index < bytes.length) {
            return Arrays.copyOfRange(bytes, 0, index);
        }
        return bytes;
    }
}
